package mqigdcom.mqigd.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

public class DBHelper extends OrmLiteSqliteOpenHelper {

    private static final String DB_NAME = "mqi_gd.db";
    private static final int DB_VERSION = 2;

    public static final String TABLE_ROAD_INFO = "road_info";
    public static final String TABLE_UNIT_INFO = "unit_info";
    public static final String TABLE_ROAD_CONDITION = "road_condition";

    public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            TableUtils.createTableIfNotExists(connectionSource, RoadInfo.class);
            TableUtils.createTableIfNotExists(connectionSource, UnitInfo.class);
            TableUtils.createTableIfNotExists(connectionSource, RoadCondition.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {
        try {
            TableUtils.dropTable(connectionSource, RoadInfo.class, true);
            TableUtils.dropTable(connectionSource, UnitInfo.class, true);
            TableUtils.dropTable(connectionSource, RoadCondition.class, true);
            onCreate(sqLiteDatabase, connectionSource);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
